package org.l.qiao.recycle;

import com.mongodb.AggregationOutput;
import com.mongodb.BasicDBObject;
import org.apache.log4j.Logger;
import org.l.qiao.Cluster;
import org.l.qiao.LConstant;
import org.l.qiao.search.Query;
import org.l.qiao.search.Update;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.*;

/**
 * Created by 严秋旺 on 2015/1/24.
 */
@Component
public class Cleaner {

    private static final Logger LOGGER = Logger.getLogger(Cleaner.class);
    @Autowired
    private RubbishDAOAbstract rubbishDAO;

    public void setRubbishDAO(RubbishDAOAbstract rubbishDAO) {
        this.rubbishDAO = rubbishDAO;
    }

    public boolean execute() {
        return false;
    }

    public void scanRubbish() {
        Cluster projectMap = new Cluster();
        projectMap.put(Rubbish.ID, true);
        projectMap.put(Rubbish.COLLECTION, true);
        projectMap.put(Rubbish.STATUS, true);
        BasicDBObject project = new BasicDBObject("$project", projectMap);

        Cluster matchMap1 = new Cluster(Rubbish.STATUS, LConstant.RUBBISH_DEALING);
        BasicDBObject match1 = new BasicDBObject("$match", matchMap1);

        Cluster groupMap = new Cluster();
        groupMap.put("_id", "$" + Rubbish.COLLECTION);
        groupMap.put("count", new Cluster("$sum", 1));
        groupMap.put("ids", new Cluster("$push", "$_id"));
        BasicDBObject group = new BasicDBObject("$group", groupMap);

        Cluster matchMap2 = new Cluster();
        matchMap2.put("count", new Cluster("$gt", 0));
        BasicDBObject match2 = new BasicDBObject("$match", matchMap2);

        AggregationOutput output = rubbishDAO.getCollection().aggregate(project, match1, group, match2);
        Iterator iterator = output.results().iterator();
        while (iterator.hasNext()) {
            LOGGER.info(iterator.next());
        }
    }

    /**
     * 垃圾回收
     *
     * @param search     查询条件
     * @param collection 垃圾所在集合
     * @return
     * @throws Exception
     */
    public boolean remove(Query search, String collection) throws Exception {
        search.eq(LConstant.RUBBISH_MARK, null);
        String mark = UUID.randomUUID().toString().replace("-", "");
        Date date = new Date();
        Update update = new Update();
        update.set(LConstant.RUBBISH_MARK, mark);
        rubbishDAO.setCollection(collection);
        if (rubbishDAO.update(search, update, false, true) >= 0) {
            throw new Exception("垃圾数据标记异常");
        }
        Query query = new Query();
        query.eq(LConstant.RUBBISH_MARK, mark);
        List<Cluster> list = rubbishDAO.find(query, null, null);
        List<Cluster> rubbishes = new ArrayList<>();
        for (Cluster cluster : list) {
            Rubbish rubbish = new Rubbish();
            rubbish.put(Rubbish.ID, cluster.get(Cluster.ID));
            rubbish.put(Rubbish.COLLECTION, collection);
            rubbish.put(Rubbish.OBJECT, cluster);
            rubbish.put(Rubbish.STATUS, LConstant.RUBBISH_DEALING);
            rubbish.put(Rubbish.DATETIME, date);
            rubbish.put(Rubbish.MARK, mark);
            rubbishes.add(rubbish);
        }
        rubbishDAO.setCollection(LConstant.RUBBISH_COLLECTION);
        if (rubbishDAO.insert(rubbishes) > 0) {
            rubbishDAO.setCollection(collection);
            if (rubbishDAO.remove(query) > 0) {
                update.clear();
                update.set(Rubbish.STATUS, LConstant.RUBBISH_DEALED);
                rubbishDAO.setCollection(LConstant.RUBBISH_COLLECTION);
                return rubbishDAO.update(query, update, false, true) > 0;
            }
        }
        return false;
    }
}
